package com.djinfo.work.dao.impl;
import com.djinfo.work.common.dao.impl.BaseDaoImpl;
import com.djinfo.work.dao.MenuDao;
import com.djinfo.work.entity.TbSysMenu;
import org.springframework.stereotype.Repository;

import java.util.List;


/**
 * 角色
 */
@Repository("MenuDao")
public class MenuDaoImpl extends BaseDaoImpl implements MenuDao {
	@Override
	public void save(TbSysMenu object) {
		if (object.getPkMenuId() == null) {
			super.save(object);
		} else {
			super.update(object);
		}
	}

	@Override
	public void update(TbSysMenu object) {
		super.update(object);
	}

	@Override
	public void delete(TbSysMenu object) {
		super.delete(this.selectOne(object));
	}

	@Override
	public void deleteMenuByParentAuthId(String id) {
		String hql = " delete FROM  TbSysMenu where parentAuthId = ? ";
		super.updateByHQL(hql,id);
	}

	@Override
	public TbSysMenu selectOne(TbSysMenu object) {
		return (TbSysMenu)super.getByPrimaryKey(TbSysMenu.class,object.getPkMenuId());
	}

	@Override
	public TbSysMenu getMenuById(String menuId) {
		return (TbSysMenu)super.getByPrimaryKey(TbSysMenu.class,menuId);
	}

	@Override
	public List<TbSysMenu> page(TbSysMenu menu, Integer pageNum, Integer pageSize) {
		if("".equals(menu.getMenuName())){
			String hql = " from TbSysMenu where parentAuthId=? order by elementIndex asc";
			return (List<TbSysMenu>)super.pageQuery( hql,  pageNum,  pageSize,menu.getParentAuthId());
		}else {
			String hql = " from TbSysMenu where parentAuthId=? and menuName like ? order by elementIndex asc";
			return (List<TbSysMenu>)super.pageQuery( hql,  pageNum,  pageSize,menu.getParentAuthId(),"%"+menu.getMenuName()+"%");
		}

	}

	@Override
	public List<TbSysMenu> list(TbSysMenu object) {
		String hql = " from TbSysMenu where parentAuthId=? order by elementIndex asc";
		return (List<TbSysMenu>)super.getList(hql,object.getPkMenuId());
	}

	@Override
	public List<TbSysMenu> list2(TbSysMenu obj) {
		return null;
	}


	@Override
	public Integer getAllRowCount() {
		String hql = "select count(*) from TbSysMenu ";
		return super.getUniqueObject(hql).hashCode();
	}

	@Override
	public Integer getRowCount(TbSysMenu object) {
		String hql="";
		if("".equals(object.getMenuName())||object.getMenuName()==null){
			hql = "select count(*) from TbSysMenu where parentAuthId=? ";
		}else {
			hql = "select count(*) from TbSysMenu where parentAuthId=? and menuName like '%"+ object.getMenuName()+ "%'";
		}

		return super.getUniqueObject(hql,object.getParentAuthId()).hashCode();
	}

	@Override
	public List<TbSysMenu> getMenuByLevel(Integer level) {
		String hql = " from TbSysMenu where level = ? ";
		return (List<TbSysMenu>)super.getList(hql,level);
	}

	@Override
	public List<TbSysMenu> getLevel1List(TbSysMenu menu) {
		String hql = " from TbSysMenu where parentAuthId=?";
		return (List<TbSysMenu>)super.getList(hql,menu.getParentAuthId());
	}

	@Override
	public List<TbSysMenu> getAllMenuList() {
		String hql = " from TbSysMenu where 1=1";
		return (List<TbSysMenu>)super.getList(hql);
	}

	@Override
	public List<TbSysMenu> getMenuListByParentId(String parentId) {
		String hql = " from TbSysMenu where 1=1 and parentAuthId=?";
		return (List<TbSysMenu>)super.getList(hql,parentId);
	}
}

